package com.zego.wawaji.ui.center.question;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.center.question.adapter.QuestionAdapter;
import com.zego.wawaji.ui.center.question.bean.QuestionList;

/**
 * 问题反馈
 * Created by Su on 2018/4/11.
 */
public class QuestionActivity extends BaseAppCompatActivity implements View.OnClickListener, RequestComplete {
    private RecyclerView qList;
    private QuestionAdapter questionAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle("问题反馈");
        setTitleRight("我的反馈", this);

        findViewById(R.id.question_add).setOnClickListener(this);
        qList = (RecyclerView) findViewById(R.id.q_list);
        qList.setLayoutManager(new LinearLayoutManager(this));
        qList.addItemDecoration(new ItemSpaces());
        qList.setHasFixedSize(true);
        qList.setNestedScrollingEnabled(false);
        questionAdapter = new QuestionAdapter();
        qList.setAdapter(questionAdapter);
        ModuleMgr.getWawaMgr().reqQuestionList(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.question_add:
                UIShow.showQuestionAddAct(this);
                break;
            case R.id.base_title_right:
                UIShow.showQuestionMyAct(this);
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqQuestionList) {
            if (!response.isOk()) {
                return;
            }

            QuestionList data = (QuestionList) response.getBaseData();
            if (data == null) return;
            questionAdapter.setList(data.getQuestionList());
        }
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.top = 10;
            outRect.bottom = 10;
        }
    }
}
